/******************************************************************************
 * * cale_global.h - data structure for calendar module
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
#ifndef CALE_GLOBAL_H
#define CALE_GLOBAL_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ui_type.h"
#include "ui_textid.h"
#include "nav.h"
#include "../../lvgl/hal/hal.h"
#include "ui_log.h"
#include "utility.h"
#include "ui_nvm_interface.h"
#include "inter_common_ui_interface.h"
#include "ui_ime.h"
#include "modem/mmi_modemadp_interface.h"
#include "inter_setting_interface.h"
#include "inter_alarm_calendar_interface.h"
#include "inter_framework_interface.h"
#include "inter_framework_calendar_interface.h"

#define UI_CALENDAR_REMINDER_TIME_INVALID      0xFF
#define NV_CALENDAR_MAX_REMINDER_NUM           10
#define UI_CALENDAR_MAX_REMINDER_INPUT_LEFT    (NV_CALENDAR_MAX_REMINDER_LEN - 1)
#define UI_CALENDAR_STATUS_DISPLAY_TIME_LEN    2000            // in ms
#define UI_CALENDAR_RING_TIME_LEN              (58 * 1000)     // in ms

#define  UI_CALE_MIN_YEAR                      1970
#define  UI_CALE_MAX_YEAR                      2037
#define  UI_CALE_YEAR_LOOP_CNT                 (UI_CALE_MAX_YEAR - UI_CALE_MIN_YEAR + 1)

typedef struct
{
    UINT8                     IndexInNV;    // index in Nvram
    NV_UI_Calendar_Reminder_t Info;
} UI_Reminder_t;

enum
{
    UI_CALE_PROCESS_NULL = 0,
    UI_CALE_PROCESS_VIEW_ALL,
    UI_CALE_PROCESS_VIEW_TODAY,
    UI_CALE_PROCESS_ADD_REMINDER
};
typedef UINT8 UI_CALE_PROCESS_T;

typedef struct
{
    VOID               *CurrentDesc;
    lv_ll_t            Reminder;            // UI_Reminder_t
    UI_Reminder_t      *AlarmReminder;      // The recent alarm for reminders, set to Alarm mode
    UI_Reminder_t      *ReminderInOper;     // the reminder in operating,
    UI_Reminder_t      *RingReminder;       // the reminder is ringing,
    TIMER_ID           StatusTimer;
    TIMER_ID           RingTimer;
    lv_calendar_date_t DayOnShow;           // to save the day on show after calendar was destoried
    lv_calendar_date_t *LunarDate;          // for lunar date searching and display
    UINT8              ReminderTxtLeft;     // char num left for reminder input
    UI_CALE_PROCESS_T  Process;
    ACTIVITY_ID        ForwardActId;
    INT8               *EditTxt;
} UI_Calendar_Mng_t;

extern UI_Calendar_Mng_t g_CalendarMng;

// common function declaration
extern UINT8 UI_Cale_Get_Weeks(lv_calendar_date_t *DayInfo);
extern UINT8 UI_Cale_Get_Weekday(lv_calendar_date_t *DayInfo);
extern VOID UI_Cale_Get_Lunar(lv_calendar_date_t *Date, INT8 *LunarDateStr, INT8 *LunarYearStr);
extern BOOL UI_Cale_IsLeapYear(INT32 tm_year);
extern VOID UI_Cale_Save_For_Add_Reminder(UINT8 Hour, UINT8 Min);
extern VOID UI_Cale_Save_For_Edit_Reminder(UINT8 Hour, UINT8 Min);
extern VOID UI_Cale_Delete_Reminder(VOID);
extern BOOL UI_Cale_Is_On_Show_Reminder_Empty(VOID);
extern UI_Reminder_t *UI_Cale_Get_Prev_Reminder(UI_Reminder_t *CurReminder, BOOL IsTodayOnly);
extern UI_Reminder_t *UI_Cale_Get_Next_Reminder(UI_Reminder_t *CurReminder, BOOL IsViewToday);
extern VOID UI_Cale_Get_YearOptions_From_Year(UINT16 Year, INT8 *YearOptions, UINT8 *Len);
extern UINT16 UI_Cale_Get_Year_From_YearOption(INT8 *pYearOption);
extern VOID UI_Cale_Input_Handler(UINT8 Len);
extern VOID UI_Cale_Alarm_Ring_Start(VOID);
extern VOID UI_Cale_Show_Lunar_Info(VOID *pDesc);
extern lv_calendar_date_t *UI_Cale_Get_Hightlighted_Dates(UINT8 *Cnt);
extern VOID UI_Cale_Display_Reminder(UI_Normal_Reminder_Desc_t *pTmpDesc, UI_Reminder_t *Reminder, BOOL IsViewToday);
extern INT8 UI_Cale_Compare_Reminder_Time(NV_UI_Calendar_Reminder_t * New, NV_UI_Calendar_Reminder_t * Old);

// display function declaration
extern VOID Display_Calendar(VOID);
extern VOID Display_Calendar_Option(VOID);
extern VOID Display_Calendar_Add_Reminder(VOID);
#if 0
extern VOID Display_Calendar_View_Today(VOID);
#endif
extern VOID Display_Calendar_Go_To_Date(VOID);
extern VOID Display_Calendar_Lunar_Cale(VOID);
extern VOID Display_Calendar_For_Chinese(VOID);
extern VOID Display_Calendar_Add_Reminder(VOID);
extern VOID Display_Calendar_Set_Alarm(VOID);
extern VOID Display_Calendar_Set_Time(VOID);
extern VOID Display_Calendar_Set_Date(VOID);
extern VOID Display_Calendar_Reminder_Added(VOID);
extern VOID Display_Calendar_Date_Past(VOID);
extern VOID Display_Calendar_View_All(VOID);
extern VOID Display_Calendar_Reminder_Memory_Empty(VOID);
extern VOID Display_Calendar_View_Option(VOID);
extern VOID Display_Calendar_Reminder_Deleted(VOID);
extern VOID Display_Calendar_View_Edit(VOID);
extern VOID Display_Calendar_Delete(VOID);
extern VOID Display_Calendar_Add_Reminder_Memory_Full(VOID);
extern VOID Display_Calendar_Add_Reminder_Memory_Full_To_Empty(VOID);
extern VOID Display_Calendar_Reminder_Alarm_Ring(VOID);
extern VOID Display_Calendar_Reminder_Discarded(VOID);
extern VOID Display_Calendar_Alarm_Postponed(VOID);

// key proc function declaration
extern UINT8 Cale_Key_Proc_Calendar(lv_obj_t *obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);
extern UINT8 Cale_Key_Proc_Cale_Opt(lv_obj_t *obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);
extern UINT8 Cale_Key_Proc_Go_To_Date(lv_obj_t *obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);
extern UINT8 Cale_Key_Proc_Add_Reminder(lv_obj_t *obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);
extern UINT8 Cale_Key_Proc_Set_Alarm(lv_obj_t *obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);
extern UINT8 Cale_Key_Proc_Set_Time(lv_obj_t *obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);
extern UINT8 Cale_Key_Proc_Set_Date(lv_obj_t *obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);
extern UINT8 Cale_Key_Proc_Add_Reminder_Mem_Full(lv_obj_t *obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);
#if 0
extern UINT8 Cale_Key_Proc_View_Today(lv_obj_t *obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);
#endif
extern UINT8 Cale_Key_Proc_View_Edit(lv_obj_t *obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);
extern UINT8 Cale_Key_Proc_Cale_View_Opt(lv_obj_t *obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);
extern UINT8 Cale_Key_Proc_Cale_Delete(lv_obj_t *obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);
extern UINT8 Cale_Key_Proc_View_All(lv_obj_t *obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);
extern UINT8 Cale_Key_Proc_Reminder_Alarm_Ring(lv_obj_t *obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);
extern UINT8 Cale_Key_Proc_Lunar_Cale(lv_obj_t *obj, UI_KEY_STATUS Key_Sta, UI_KEY_VALUE Key_Val);

// release proc declaration
extern void Cale_Rel_Func_Add_Reminder(lv_obj_t *Obj,lv_event_t event);
extern void Cale_Rel_Func_View_Today(lv_obj_t *Obj,lv_event_t event);
extern void Cale_Rel_Func_Go_To_Date(lv_obj_t *Obj,lv_event_t event);
extern void Cale_Rel_Func_Lunar_Cale(lv_obj_t *Obj,lv_event_t event);
extern void Cale_Rel_Func_Alarm_On(lv_obj_t *Obj,lv_event_t event);
extern void Cale_Rel_Func_Alarm_Off(lv_obj_t *Obj,lv_event_t event);
extern void Cale_Rel_Func_View_Option_Delete(lv_obj_t *Obj,lv_event_t event);
extern void Cale_Rel_Func_View_Option_Edit(lv_obj_t *Obj,lv_event_t event);
extern void Cale_Rel_Func_View_All(lv_obj_t *Obj,lv_event_t event);

extern lv_res_t UI_Cale_RollerMonth_SelAction(lv_obj_t *Roller);
extern lv_res_t UI_Cale_RollerYear_SelAction(lv_obj_t *Roller);

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif /* CALE_GLOBAL_H */
